//#include "StdAfx.h"
#include "Tree.h"
#include "ObjReader.h"
#include "D3DApp.h"
#include "DX10ObjMesh.h"
#include "Level.h"

DX10ObjMesh* Tree::m_pDrawMesh = 0;

Tree::Tree(D3DXVECTOR3 pos, D3DApp *app):
	LevelElement(),		
	m_Position(pos),
	m_app(app)
{
}

Tree::~Tree(void)
{
}

void Tree::Draw(const RenderContext* pRenderContext, ID3D10ShaderResourceView* shadowdepthmap)
{
	m_pDrawMesh->SetWorldMatrix(GetWorldMatrix());
	m_pDrawMesh->Draw(pRenderContext);
}

void Tree::Tick(const InputState& inputState)
{		
}


void Tree::Initialize(ContentManager* manager)
{
	Rotate(D3DXVECTOR3(-1.57f,0,0));
	Translate(m_Position);
}

void Tree::BuildDrawMesh(D3DApp* app, Level* plevel, ContentManager *pContentManager)
{
	IniFile* config = new IniFile();
	config->SetValue(_T("Export"),_T("baseDir"),_T("."));
	config->SetValue(_T("Export"),_T("meshDir"),_T(""));
	config->SetValue(_T("Export"),_T("textureDir"),_T(""));
	config->SetValue(_T("Export"),_T("skeletonDir"),_T(""));

	ObjReader reader(config,_T("./Models/Tree/tree.obj"));
	if ( reader.Read() )
	{
		MeshList* list = new MeshList();
		reader.CopyIntoMeshList(list);
		vector<ObjMesh*>::const_iterator it;
		for (it=list->begin(); it!= list->end(); it++)
		{
			ObjMesh* mesh = *it;
			// standaard shader meegeven
			DX10ObjMesh* dx10Mesh = app->Compile(mesh,_T("Tree.fx"), _T("AlphaBlend"));
			if ( dx10Mesh != 0 )
			{
				m_pDrawMesh = dx10Mesh;
				
			}
		}
		m_pDrawMesh->EnableDraw(true);
		m_pDrawMesh->SetParent(plevel);
		m_pDrawMesh->Initialize(pContentManager);
	}
	delete config;
}

void Tree::ReleaseDrawMesh()
{
	SafeDelete(m_pDrawMesh);
}